package com.imti.contentproviderdemo;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.text.TextUtils;
import java.util.List;

public class MyContentProvider extends ContentProvider
{

    private static final String MIME_ITEM = "vnd.android.cursor/com.imti.student.item";

    private static final String MIME_DIR = "vnd.android.cursor/com.imti.student.dir";

    private static final int OP_DIR = 0xf1;

    private static final int OP_ITME = 0xf2;

    private static UriMatcher sMatcher;

    private MyDBHelper mHelper;

    static
    {
        //初始化UriMatcher
        sMatcher = new UriMatcher(UriMatcher.NO_MATCH);

        //添加需要匹配的Uri
        //当匹配器匹配成功某个Uri时，会返回注册的code，即参数3
        sMatcher.addURI(StuUri.AUTHORTIES, "/deleteStu", OP_DIR);
        //若单条操作时，Uri后续拼接的为表对应的主键ID，则通过通配符#标识
        sMatcher.addURI(StuUri.AUTHORTIES, "/deleteSingleStu/#", OP_ITME);
    }

    //ContentProvider初始化的回调方法
    //当onCreate()返回true时，表示ContentProvider创建成功
    //ContentProvider创建成功后，不会重复创建
    @Override
    public boolean onCreate()
    {
        mHelper = new MyDBHelper(
            getContext(), "student.db", null, 1);

        return mHelper.getWritableDatabase() != null;
    }

    //当外部通过ContentResolver调用insert()方法时触发
    //return The URI for the newly inserted item.
    @Override
    public Uri insert(Uri uri, ContentValues values)
    {
        //获取Uri的path部分，以/分隔而成的字符串List
        List<String> pathList = uri.getPathSegments();

        //获取Uri的path部分最后一位
        String insertOpFlag = pathList.get(pathList.size() - 1);

        switch (insertOpFlag)
        {
            //由此判断为具体的某个Uri操作
            case "addStu":
                long id = mHelper.insertStuInfo(values);
                return Uri.parse("content://" + StuUri.AUTHORTIES + "/" + id);
        }

        return null;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs)
    {
        //判断删除单条数据还是多条数据
        switch (getType(uri))
        {
            case MIME_ITEM:

                break;
        }

        //获取Uri的path部分，以/分隔而成的字符串List
        List<String> pathList = uri.getPathSegments();

        //获取Uri的path部分最后一位
        String deleteOpFlag = pathList.get(pathList.size() - 1);

        //selection的内容很可能是：stu_name=? or stu_class=?
        //可直接使用的为stu_name=?
        switch (deleteOpFlag)
        {
            case "deleteStu":
                if (!TextUtils.isEmpty(selection))
                {
                    if (!selection.contains("and") && !selection.contains("or"))
                    {
                        if (selection.contains(MyDBHelper.STU_NAME))
                        {
                            return mHelper.deleteStuByName(selectionArgs[0]);
                        }

                    } else
                    {
                        //TODO：Where语句多字段操作
                    }
                }
                break;

        }
        return -1;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
        String[] selectionArgs)
    {
        // TODO: Implement this to handle requests to update one or more rows.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
        String[] selectionArgs, String sortOrder)
    {
        // TODO: Implement this to handle query requests from clients.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    //RecyclerView.Adapter.getItemViewType(int position)
    @Override
    public String getType(Uri uri)
    {
        //匹配器通过匹配注册的Uri对应的code值，返回单条或多条操作
        switch (sMatcher.match(uri))
        {
            case OP_ITME:
                return MIME_ITEM;

            case OP_DIR:
                return MIME_DIR;

            default:
                return null;
        }
    }
}